﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KnapsackProblem
{
    public partial class formMain : Form
    {
        private int[] w = new int[101];
        private int[] v = new int[101];
        private bool IsNumberEnter = false;

        public formMain()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Check Is Number Entered
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbTotal_KeyDown(object sender, KeyEventArgs e)
        {
            IsNumberEnter = false;
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
                // Determine whether the keystroke is a number from the keypad.
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    // Determine whether the keystroke is a backspace.
                    if (e.KeyCode != Keys.Back)
                    {
                        // A non-numerical keystroke was pressed.
                        // Set the flag to true and evaluate in KeyPress event.
                        IsNumberEnter = true;
                    }
                }

        }

        /// <summary>
        /// Display Number Only
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbTotal_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (IsNumberEnter == true)
            {
                e.Handled = true;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //check infomation
            int i = int.Parse(this.lbIndexDegit.Text);
            int n = 0;
            if (this.cbTotal.Text.Length > 0)
            {
                n = int.Parse(this.cbTotal.Text);
            }
            if (i <= n)
            {
                int wi = 0, vi = 0;
                if (this.cbWeight.Text.Length > 0)
                {
                    wi = int.Parse(this.cbWeight.Text);
                    if (this.cbValue.Text.Length > 0)
                    {
                        vi = int.Parse(this.cbValue.Text);
                        w[i] = wi;
                        v[i] = vi;                        
                        ListViewItem lvi = new ListViewItem();
                        lvi.SubItems.Add(i.ToString());
                        lvi.SubItems.Add(wi.ToString()+ " kg");
                        lvi.SubItems.Add(vi.ToString() + " $");
                        this.lvThingsDetail.Items.Add(lvi);
                        lbIndexDegit.Text = (i + 1).ToString();
                    }
                }
            }
            else
            {
                return;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Addlistview column
            this.lvThingsDetail.View = View.Details;
            this.lvThingsDetail.CheckBoxes = true;
            this.lvThingsDetail.Columns.Add("Result", 70, HorizontalAlignment.Center);
            this.lvThingsDetail.Columns.Add("Index", 130, HorizontalAlignment.Left);
            this.lvThingsDetail.Columns.Add("Weight", 130, HorizontalAlignment.Left);
            this.lvThingsDetail.Columns.Add("Value", 140, HorizontalAlignment.Left);
            //create data
            for (int i = 1; i <= 10; i++)
            {
                this.cbWeight.Items.Add(i.ToString());
            }
            for (int i = 1; i <= 100; i++)
            {
                this.cbTotal.Items.Add(i.ToString());
                this.cbMaxWeight.Items.Add(i.ToString());
                this.cbValue.Items.Add(i.ToString());
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            this.lvThingsDetail.Items.Clear();
            this.lbIndexDegit.Text = "1";
            this.cbTotal.Text = "";
            this.cbMaxWeight.Text = "";
            this.cbWeight.Text = "";
            this.cbValue.Text = "";
            this.lbResult.Text = "0 $";
            this.w = new int[101];
            this.v = new int[101];
        }

        private void btnRandom_Click(object sender, EventArgs e)
        {
            this.lvThingsDetail.Items.Clear();
            Random random = new Random();
            int n = 0;
            if (this.cbTotal.Text.Length > 0)
            {
                n = int.Parse(this.cbTotal.Text);
            }
            for (int i = 1; i <= n; i++)
            {
                this.lbIndexDegit.Text = i.ToString();
                w[i] = random.Next(1, 10);
                v[i] = random.Next(1, 100);
                ListViewItem lvi = new ListViewItem();
                lvi.SubItems.Add(i.ToString());
                lvi.SubItems.Add(w[i].ToString()+" kg");
                lvi.SubItems.Add(v[i].ToString()+ " $");
                this.lvThingsDetail.Items.Add(lvi);
            }
        }

        private void btnTest_Click(object sender, EventArgs e)
        {            

            int n = 0;
            if (this.cbTotal.Text.Length > 0)
            {
                n = int.Parse(this.cbTotal.Text);
            }
            else
            {
                return;
            }
               
            int M = 0;
            if (this.cbMaxWeight.Text.Length > 0)
            {
                M = int.Parse(this.cbMaxWeight.Text);
            }
            else
            {                    
                return;
            }

            if (this.lvThingsDetail.Items.Count != n)
            {                   
                return;
            }
            else
            {                   
                Knapsack knapsack = new Knapsack(n, M, w, v);                       
                knapsack.Optimize();                   
                this.lbResult.Text = knapsack.GetMaxValue().ToString() + " $";
                int[] trace = new int[101];
                trace = knapsack.Trace();                        
                for (int i = 1; i <= n; i++)
                {
                    if (trace[i] == 1)
                    {
                        this.lvThingsDetail.Items[i - 1].Checked = true;
                        this.lvThingsDetail.Items[i - 1].BackColor = Color.BlueViolet;
                    }
                    else
                    {
                        this.lvThingsDetail.Items[i - 1].Checked = false;
                        this.lvThingsDetail.Items[i - 1].BackColor = Color.White;
                    }
                }                   
            }
            this.lvThingsDetail.Update();
        }

    }
}
